//
//  MXMessageTest.m
//  messageTest
//
//  Created by muxue on 2019/4/13.
//  Copyright © 2019 暮雪. All rights reserved.
//

#import "MXMessageTest.h"
#import <objc/runtime.h>

@implementation MXMessageTest

// 动态添加的方法
void dynamicMethodIMP(id self, SEL _cmd) {
    NSLog(@">>>>>>>>>>>>>>>%s", __func__);
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    NSLog(@">>>>>>>>>>>>>>>%s", __func__);
//    if ([NSStringFromSelector(sel) isEqualToString:@"length"]) {
//         class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "v@:");
//        return YES;
//    }
    return [super resolveInstanceMethod:sel];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSLog(@">>>>>>>>>>>>>>>%s", __func__);
    return @"1234";
}


- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@">>>>>>>>>>>>>>>%s>>>>%@", __func__, [anInvocation.target class]);
//    anInvocation.target = @"1234";
    [anInvocation invoke];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSLog(@">>>>>>>>>>>>>>>%s", __func__);
    if ([NSStringFromSelector(aSelector) isEqualToString:@"length"]) {
        class_addMethod([self class], aSelector, (IMP)dynamicMethodIMP, "v@:");
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }

    return [super methodSignatureForSelector:aSelector];
}

@end
